package de.justphil.tcg.tcgserver.game.mechanics.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.JTextArea;

public class Slot extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel labelLifeValue;
	private JLabel labelAttackValue;
	private JLabel labelDefenseValue;
	private JLabel labelSpeedValue;

	private JTextArea textAreaProperties;

	private JPanel panelImage;

	private static final Color COLOR_EMPTY = Color.WHITE;
	private static final Color COLOR_ACTIVE = Color.RED;
	private static final Color COLOR_PASSIVE = Color.ORANGE;
	private static final Color COLOR_WINNER = Color.GREEN;

	/**
	 * Create the panel.
	 */
	public Slot() {

		setBackground(Color.BLACK);
		setLayout(new GridLayout(2, 1, 0, 5));

		panelImage = new JPanel();
		panelImage.setBackground(Color.BLACK);
		add(panelImage);

		textAreaProperties = new JTextArea();
		textAreaProperties.setText("Properties:");
		panelImage.add(textAreaProperties);

		JPanel panelData = new JPanel();
		panelData.setBackground(Color.BLACK);
		add(panelData);
		panelData.setLayout(new GridLayout(4, 0, 0, 2));

		JPanel panelDataLife = new JPanel();
		panelData.add(panelDataLife);
		panelDataLife.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panelDataLifeLabel = new JPanel();
		panelDataLifeLabel.setBackground(new Color(255, 150, 150));
		panelDataLife.add(panelDataLifeLabel);

		JLabel labelLifeLabel = new JLabel("Life:");
		panelDataLifeLabel.add(labelLifeLabel);

		JPanel panelDataLifeValue = new JPanel();
		panelDataLife.add(panelDataLifeValue);

		this.labelLifeValue = new JLabel("");
		panelDataLifeValue.add(this.labelLifeValue);

		JPanel panelDataAttack = new JPanel();
		panelData.add(panelDataAttack);
		panelDataAttack.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panelDataAttackLabel = new JPanel();
		panelDataAttackLabel.setBackground(new Color(150, 150, 255));
		panelDataAttack.add(panelDataAttackLabel);

		JLabel labelAttackLabel = new JLabel("Attack:");
		panelDataAttackLabel.add(labelAttackLabel);

		JPanel panelDataAttackValue = new JPanel();
		panelDataAttack.add(panelDataAttackValue);

		this.labelAttackValue = new JLabel("");
		panelDataAttackValue.add(labelAttackValue);

		JPanel panelDataDefense = new JPanel();
		panelData.add(panelDataDefense);
		panelDataDefense.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panelDataDefenseLabel = new JPanel();
		panelDataDefenseLabel.setBackground(new Color(200, 200, 200));
		panelDataDefense.add(panelDataDefenseLabel);

		JLabel labelDefenseLabel = new JLabel("Defense:");
		panelDataDefenseLabel.add(labelDefenseLabel);

		JPanel panelDataDefensValue = new JPanel();
		panelDataDefense.add(panelDataDefensValue);

		labelDefenseValue = new JLabel("");
		panelDataDefensValue.add(labelDefenseValue);

		JPanel panelDataSpeed = new JPanel();
		panelData.add(panelDataSpeed);
		panelDataSpeed.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panelDataSpeedLabel = new JPanel();
		panelDataSpeedLabel.setBackground(new Color(150, 255, 150));
		panelDataSpeed.add(panelDataSpeedLabel);

		JLabel labelSpeedLabel = new JLabel("Speed:");
		panelDataSpeedLabel.add(labelSpeedLabel);

		JPanel panelDataSpeedValue = new JPanel();
		panelDataSpeed.add(panelDataSpeedValue);

		labelSpeedValue = new JLabel("");
		panelDataSpeedValue.add(labelSpeedValue);

		setEmty();
	}

	public String getLifeValue() {
		return this.labelLifeValue.getText();
	}

	public String getAttackValue() {
		return this.labelAttackValue.getText();
	}

	public String getDefenseValue() {
		return this.labelDefenseValue.getText();
	}

	public String getSpeedValue() {
		return this.labelSpeedValue.getText();
	}

	public void setLifeValue(String text) {
		this.labelLifeValue.setText(text);
	}

	public void setAttackValue(String text) {
		this.labelAttackValue.setText(text);
	}

	public void setDefenseValue(String text) {
		this.labelDefenseValue.setText(text);
	}

	public void setSpeedValue(String text) {
		this.labelSpeedValue.setText(text);
	}

	public void setProperties(List<String> properties) {

		String text = "";

		if (properties != null && properties.size() > 0) {
			Map<String, Integer> propertyCounter = new HashMap<String, Integer>();

			text = properties.remove(0) + "\n";

			for (String propertyString : properties) {

				Integer count = propertyCounter.get(propertyString);
				if (count == null) {
					propertyCounter.put(propertyString, 1);
				} else {
					propertyCounter.put(propertyString, count + 1);
				}
			}

			Collection<String> propertyKeys = propertyCounter.keySet();

			for (String propertyKey : propertyKeys) {

				text += propertyKey;
				if (propertyCounter.get(propertyKey) > 1) {
					text += " x " + propertyCounter.get(propertyKey);
				}
				text += "\n";
			}
		}
		this.textAreaProperties.setText(text);

	}

	public void setActive(boolean active) {
		
		if (active) {
			panelImage.setBackground(COLOR_ACTIVE);

		} else {
			panelImage.setBackground(COLOR_PASSIVE);
		}
	}

	public void setWinner() {
		
			panelImage.setBackground(COLOR_WINNER);
	}

	public void setEmty() {

		panelImage.setBackground(COLOR_EMPTY);
		setLifeValue("");
		setAttackValue("");
		setDefenseValue("");
		setSpeedValue("");
		setProperties(new LinkedList<String>());
	}

	public void setCard(String life, String attack, String defense,
			String speed, List<String> properties) {

		if (panelImage.getBackground() == COLOR_EMPTY) {

			panelImage.setBackground(COLOR_PASSIVE);
		}
		
		setLifeValue(life);
		setAttackValue(attack);
		setDefenseValue(defense);
		setSpeedValue(speed);
		setProperties(properties);
	}
}
